home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 352_01 / strppcat.cpp < prev    next >
C/C++ Source or Header  |  1991-04-22  |  1KB  |  58 lines

  1. // STRPPCAT.CPP - string concatenation routines.
  2. //        module includes String::operator+= and String::operator+
  3. //
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <alloc.h>
  7.  
  8. #include "wtwg.h"            // for wmalloc()
  9. #include "dblib.h"
  10.  
  11.     
  12.     
  13. String& operator+=(String& a, String& b)
  14.     {
  15.     char *oldptr, *newptr;
  16.     register int newlen, oldlen, sourcelen;
  17.  
  18.     sourcelen = b.n;
  19.     oldptr = a.s;                    // hold orig. string info.
  20.     oldlen =a.n;
  21.     newlen =oldlen +sourcelen;            // compute larger string size.
  22.     newptr = (char *) wmalloc(newlen+1, "String");
  23.     if ( oldlen >0 )  memcpy ( newptr, oldptr, oldlen );
  24.     memcpy ( newptr+oldlen, b.s, sourcelen );
  25.     newptr[newlen] = 0;
  26.     if ( oldptr != NULL ) free (oldptr);
  27.     a.s = newptr;
  28.     a.n = newlen;
  29.     return a;
  30.     }
  31.  
  32. String operator+(String& a, String& b)
  33.     {
  34.     String tmp =a;
  35.     tmp +=b;         // use of operator += defined above
  36.     return tmp; 
  37.     }
  38.     
  39. String& operator+=(String& a, char *b)
  40.     {
  41.     String Sb = b;
  42.     a+= Sb;
  43.     return a;
  44.     }
  45.     
  46. String  operator+(String& a, char *b)
  47.     {
  48.     String Sb = b;
  49.     return (a+Sb);            
  50.     }
  51.             
  52. String  operator+(char *a, String& b)
  53.     {
  54.     String Sa = a;
  55.     return (Sa+b);            
  56.     }
  57.             
  58. //--------------- end of STRPPCAT.CPP ----------------------